package com.foot.travel.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.foot.travel.R;
import com.koalac.ironman.cache.SysAppConfig;
import com.koalac.ironman.common.module.BaseActivity;
import com.koalac.ironman.net.MyRequest;
import com.koalac.ironman.net.ResponseCallback;
import com.koalac.ironman.net.pojo.request.CLogin;
import com.koalac.ironman.net.pojo.response.Response;
import com.koalac.ironman.net.pojo.response.User;
import com.koalac.ironman.util.ScreenInfoUtil;
import com.koalac.ironman.util.StringUtils;
import com.koalac.ironman.viewhelper.ViewInject;
import com.koalac.ironman.weixin.FootTravelWechat;
import com.koalac.ironman.widget.LoadingDialog;

public class LoginActivity extends BaseActivity implements OnClickListener {

    @ViewInject(id = R.id.parent_layout)
    View mParentLayout;
    @ViewInject(id = R.id.user_name)
    EditText mUserNameView;
    @ViewInject(id = R.id.password)
    EditText mPasswordView;
    @ViewInject(id = R.id.next_btn, click = "onClick")
    TextView mNextBtn;
    @ViewInject(id = R.id.tourist, click = "onClick")
    TextView mTouristBtn;
    @ViewInject(id = R.id.register, click = "onClick")
    TextView mRegisterBtn;
    @ViewInject(id = R.id.wechat, click = "onClick")
    ImageView mWechatBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_login);
	User user = SysAppConfig.getInstance().getUser(LoginActivity.this);
	if (user != null
		&& !StringUtils.isEmpty(SysAppConfig.getInstance().getToken(
			LoginActivity.this))) {
	    startActivity(new Intent(LoginActivity.this, MainActivity.class));
	    LoginActivity.this.finish();
	}
	int bottomHeight = ScreenInfoUtil.getScreenHeight(LoginActivity.this)
		- ScreenInfoUtil.dp2px(LoginActivity.this, 530);
	checkKeyboardHeight(mParentLayout, bottomHeight);
    }

    private boolean checkInput() {
	if (StringUtils.isEmpty(mUserNameView.getText().toString())) {
	    toast("请填写用户名!");
	    return false;
	}
	if (StringUtils.isEmpty(mPasswordView.getText().toString())) {
	    toast("请填写密码!");
	    return false;
	}
	return true;
    }

    @Override
    public void onClick(View v) {
	int viewId = v.getId();
	switch (viewId) {
	case R.id.next_btn:
	    if (checkInput()) {
		/**
		 * 登录
		 */
		CLogin cLogin = new CLogin();
		cLogin.setUserName(mUserNameView.getText().toString());
		cLogin.setPassword(mPasswordView.getText().toString());
		cLogin.setDeviceId(getRegistrationID(LoginActivity.this));
		final LoadingDialog loadingDialog = showLoadingDialog("正在登录...");
		MyRequest.request(LoginActivity.this, cLogin,
			new ResponseCallback() {

			    @Override
			    public void process(boolean result,
				    Context context, Response data, String msg) {
				toast(msg);
				canceledLoadingDialog(loadingDialog);
				if (result) {
				    canceledLoadingDialog(loadingDialog);
				    User user = data.getObject(User.class);
				    SysAppConfig.getInstance().setUser(
					    LoginActivity.this, user);
				    SysAppConfig.getInstance().setToken(
					    context, user.getToken());
				    Log.i("YYYYYYYYYYY=========", "====="
					    + user.getToken());
				    startActivity(new Intent(
					    LoginActivity.this,
					    MainActivity.class));
				    LoginActivity.this.finish();
				}
			    }
			});
	    }
	    break;
	case R.id.register:
	    startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
	    break;
	case R.id.tourist:
	    startActivity(new Intent(LoginActivity.this, MainActivity.class));
	    break;
	case R.id.wechat:
	    FootTravelWechat.INSTANCE.login();
	    break;
	}
    }

}
